/*
 * @Description: 
 * @Author: yyh
 * @Date: 2022-03-30 00:33:34
 * @LastEditors: yyh
 * @LastEditTime: 2022-05-16 12:01:11
 */
var db = require("../utils/db");
var url = require("url");
var qs = require("querystring");

//新增使用资产数据
addUseInfo =  async (req,res) => {
    var {
        asset_id,
        asset_name,
        use_department,
        use_time,
        status,
        operator
    } = req.body;
    var sql = "insert into `use` (asset_id,asset_name,use_department,use_time,status,operator) values(?,?,?,?,?,?)";
    var sqlArr = [
        asset_id,
        asset_name,
        use_department,
        use_time,
        status,
        operator
    ];
    var callBack = async (err, data) => {
      if(err) {
          console.log(err);
          res.send({
              code: 400,
              msg: "新增失败"
          })
      } else {
          res.send({
              code: 200,
              msg: "新增成功",
              data: data
          })
      }
  }
  db.sqlConnect(sql,sqlArr,callBack);
}


//删除使用资产数据
deleteUseInfo = async (req, res) => {
    var id = req.query[0];
    var sql = "delete from `use` where id = ?";
    var sqlArr = [id];
    var result = await db.SySqlConnect(sql, sqlArr);
    if (result.affectedRows == 1) {
      res.send({
        code: 200,
        msg: "删除成功"
      });
    } else {
      res.send({
        code: 400,
        msg: "删除失败"
      });
    }
  };
  
  //修改使用资产数据
  updateUseInfo = async (req, res) => {
    var {
      id,
      asset_id,
      asset_name,
      use_department,
      use_time,
      status,
      operator
    } = req.body;
    var sql =
      "update `use` set asset_id=?,asset_name=?,use_department=?,use_time=?,status=?,operator=? where id=?";
    var sqlArr = [
      asset_id,
      asset_name,
      use_department,
      use_time,
      status,
      operator,
      id
    ];
    var result = await db.SySqlConnect(sql, sqlArr);
    if (result.affectedRows == 1) {
      res.send({
        code: 200,
        msg: "修改成功"
      });
    } else {
      res.send({
        code: 400,
        msg: "修改失败"
      });
    }
  }

  //通过id修改使用资产状态
  updateUseStatus = async (req, res) => {
    var {
      id,
      status
    } = req.body;
    var sql =
      "update `use` set status=? where id=?";
    var sqlArr = [
      status,
      id
    ];
    var result = await db.SySqlConnect(sql, sqlArr);
    if (result.affectedRows == 1) {
      res.send({
        code: 200,
        msg: "修改成功"
      });
    } else {
      res.send({
        code: 400,
        msg: "修改失败"
      });
    }
  };
  
  //模糊查询、分页查询使用资产数据
  getUseInfo = async (req,res) => {
    var params = url.parse(req.url).query;
    params = qs.parse(params);
    var sql = "select * from `use`";
    var sqlArr = [];
    var isMore = false; //是否有多个查询参数
    if(params.asset_id){
        sql += " where asset_id like ?";
        sqlArr.push("%" + params.asset_id + "%");
        isMore = true;
    }
    if (params.status) {
      if (isMore) {
        sql += " and status like ?";
      } else {
        sql += " where status like ?";
        isMore = true;
      }
      sqlArr.push("%" + params.status + "%");
    }
    if (params.startTime && params.endTime) {
      if (isMore) {
        sql += " and use_time between ? and ?";
      } else {
        sql += " where use_time between ? and ?";
      }
      sqlArr.push(params.startTime, params.endTime);
    }
    if(params.page && params.pageSize) {
        sql += " limit ?,?";
        sqlArr.push((params.page - 1)* params.pageSize, parseInt(params.pageSize));
    }
    var callBack = async (err, data) => {
        if(err) {
            console.log(err);
            res.send({
                code: 400,
                msg: "模糊查询失败"
            })
        } else {
            res.send({
                code: 200,
                msg: "模糊查询成功",
                data: data
            })
        }
    }
    db.sqlConnect(sql,sqlArr,callBack);
  };

  //通过id查询使用资产数据
  getUseInfoById = async (req,res) => {
    var id = req.query[0];
    var sql = "select * from `use` where id=?";
    var sqlArr = [id];
    var callBack = async (err, data) => {
      if(err) {
          console.log(err);
          res.send({
              code: 400,
              msg: "查询失败"
          })
      } else {
          res.send({
              code: 200,
              msg: "查询成功",
              data: data
          })
      }
  }
  db.sqlConnect(sql,sqlArr,callBack);
  }

module.exports = {
    addUseInfo,
    deleteUseInfo,
    updateUseInfo,
    updateUseStatus,
    getUseInfo,
    getUseInfoById
}